/**
 * 
 * @param {*} date1 '2023-07-19'
 * @param {*} date2 '2023-07-21'
 */
export function calculateTimeDifference(date1, date2) {
    // 将时间字符串转换为日期对象
    var date1Obj = new Date(date1);
    var date2Obj = new Date(date2);

    // 计算时间差（毫秒）
    var timeDiff = date2Obj.getTime() - date1Obj.getTime();

    // 将毫秒转换为天数
    var daysDiff = timeDiff / (1000 * 3600 * 24);

    // 返回时间差的绝对值（确保结果为正数）
    return Math.abs(Math.round(daysDiff));
}

/**
 * var dateRanges = [['2023-07-19', '2023-07-21'], ['2023-07-19', '2023-07-22'],['2023-07-19', '2023-07-23']];-----3天
 * @param {*} arr 以上格式的数组 （可以过滤掉重复日期，
 */
export function calculateTotalTime(arr) {
    var totalDays = 0;
    let dateRanges = arr.filter(item => item.length != 0)
    for (var i = 0; i < dateRanges.length; i++) {
        var startDate = new Date(dateRanges[i][0]);
        var endDate = new Date(dateRanges[i][1]);

        var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24)) + 1;
        totalDays += daysDiff;

        // 减去与后续日期范围的重叠天数
        for (var j = i + 1; j < dateRanges.length; j++) {
            var nextStartDate = new Date(dateRanges[j][0]);
            var nextEndDate = new Date(dateRanges[j][1]);

            var overlapStart = Math.max(startDate, nextStartDate);
            var overlapEnd = Math.min(endDate, nextEndDate);

            if (overlapStart <= overlapEnd) {
                var overlapDays = Math.ceil((overlapEnd - overlapStart) / (1000 * 3600 * 24)) + 1;
                totalDays -= overlapDays;
            }
        }
    }
    return totalDays;
}

/**
 * 传入日期，获取之后n天的日期
 * @param {date} n 当前日期
 * @param {int} days n天后日期
 * @param {bool} before true,获取当前日期之前的日期，不传入则获取之后的日期
 * @return 返回days后的日期
 */

export function getdaysAgo(n, days, before) {
    let startDate = new Date(n);
    let endDate = new Date(startDate.getTime() + (days * 24 * 60 * 60 * 1000));

    if (before == true) {
        endDate = new Date(startDate.getTime() - (days * 24 * 60 * 60 * 1000));
    }
    let year = endDate.getFullYear();
    let month = String(endDate.getMonth() + 1).padStart(2, "0");
    let day = String(endDate.getDate()).padStart(2, "0");
    let formattedEndDate = `${year}-${month}-${day}`;
    return formattedEndDate
}
/**
 *  获取开始日期和结束日期期间的数组
 * @param {*} startDate 开始日期
 * @param {*} endDate 结束日期
 */

export function getDateArr(startDate, endDate) {
    startDate = new Date(startDate)
    endDate = new Date(endDate)
    const dates = [];
    let currentDate = startDate;
    while (currentDate <= endDate) {
        const year = currentDate.getFullYear();
        const month = String(currentDate.getMonth() + 1).padStart(2, "0");
        const day = String(currentDate.getDate()).padStart(2, "0");
        const formattedDate = `${year}-${month}-${day}`;
        dates.push(formattedDate);

        currentDate.setDate(currentDate.getDate() + 1);
    }
    // console.log(dates);
    return dates
}
/**
* 2021-11-22格式转11-22格式
* @param {*} val 2021-11-22
* @param {bool} isDiagonal 是否斜杠
*/
export function handleDate(val, isDiagonal) {
    if (val) {
        val = val.substring(5)
        if (isDiagonal) {
            val = val.replace('-', '/')
        }
        return val
    }
}

/**
 * 转换周*
 * @param {*} val 
 */
export function handlweek(val) {
    let week = ''
    val = new Date(val).getDay()
    switch (val) {
        case 0:
            week = "日"
            break;
        case 1:
            week = "一"
            break;
        case 2:
            week = "二"
            break;
        case 3:
            week = "三"
            break;
        case 4:
            week = "四"
            break;
        case 5:
            week = "五"
            break;
        case 6:
            week = "六"
            break;
    }
    return week
}
/**
* 转换成字符串格式
* @param {*} date 
* @param {*} key true返回MM-dd
*/
export function formatDate(date, key) {
    let year = date.getFullYear();
    let month = String(date.getMonth() + 1).padStart(2, '0');
    let day = String(date.getDate()).padStart(2, '0');
    let hours = String(date.getHours()).padStart(2, '0');
    let minutes = String(date.getMinutes()).padStart(2, '0');
    let seconds = String(date.getSeconds()).padStart(2, '0');

    if (key == 1) {
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    } else if (key == 2) {
        return `${month}-${day} ${hours}:${minutes}`;
    } else {
        return `${year}-${month}-${day}`;
    }
}

/**
 * 给一组日期，排序
 * @param {*} dates 日期数组
 */
export function dateSort(dates) {
    dates.sort((a, b) => {
        let dateA = new Date(a);
        let dateB = new Date(b);
        return dateA.getTime() - dateB.getTime();
    });
    return dates
}